﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace QuanLyBanVeXe
{
    public partial class FormTuyenDi : DevComponents.DotNetBar.Office2007RibbonForm
    {

        public SqlConnection myConnection;
        public SqlCommand myCommand;
        SqlDataAdapter myDataAdapter;
        public FormTuyenDi()
        {
            InitializeComponent();
        }

        public void KetNoi()
        {
            string strconnect = @"server=.\SQLEXPRESS; database=QuanLyBanVeXe; integrated security=true";
            myConnection = new SqlConnection(strconnect);
            myCommand = myConnection.CreateCommand();
        }
        private void FormTuyenDi_Load(object sender, EventArgs e)
        {
            btLoad_Click(sender, e);
            cbbLoaiXe.Text = null;
            cbbTimLoaiXe.DataSource = LayLoaiXe();
            cbbTimLoaiXe.DisplayMember = "LoaiXe";
            cbbTimLoaiXe.Text = null;
            btLuu.Visible = false;
        }
        private void FormTuyenDi_FormClosed(object sender, FormClosedEventArgs e)
        {
            this.Dispose();
            FormMain frm = new FormMain();
            frm.Show();
            
        }

        private void btLoad_Click(object sender, EventArgs e)
        {
            LoadList(sender,e);
            ssThongBao.Text = "Thông báo";
            dgvListTuyenDi.Enabled = true;
            btThemTuyen.Visible = true;
            cbbLoaiXe.DataSource = LayLoaiXe();
            cbbLoaiXe.DisplayMember = "LoaiXe";
            cbbLoaiXe.Text = null;
        }
        private void LoadList(object sender, EventArgs e)
        {
            KetNoi();
            myCommand.CommandType = CommandType.Text;
            myCommand.CommandText = "select  * FROM TuyenDi";
            try
            {
                if (this.myConnection.State == ConnectionState.Closed)
                    this.myConnection.Open();
                myDataAdapter = new SqlDataAdapter(myCommand);
                DataSet ds = new DataSet();
                myDataAdapter.Fill(ds, "TuyenDi");
                this.dgvListTuyenDi.DataSource = ds.Tables[0];
                this.myConnection.Close();
            }
            catch
            {
                MessageBox.Show("Lối kết nối");
            }
            
            ssCount.Text = " || Số tuyến: " + (dgvListTuyenDi.RowCount - 1).ToString();
            btCapNhat.Enabled = false;
            btXoa.Enabled = false;
            btLuu.Visible = true;
            
        }
        private void dgvListTuyenDi_RowPrePaint(object sender, DataGridViewRowPrePaintEventArgs e)
        {
            dgvListTuyenDi.Rows[e.RowIndex].Cells["STT"].Value = e.RowIndex + 1;
        }
        string ben, loaixe;
        private void dgvListTuyenDi_Click(object sender, EventArgs e)
        {
            if (this.dgvListTuyenDi.SelectedRows.Count > 0)
            {
                try
                {
                    for (int i = dgvListTuyenDi.SelectedRows.Count - 1; i >= 0; i--)
                    {

                        ben = dgvListTuyenDi.SelectedRows[i].Cells[2].Value.ToString();
                        loaixe = dgvListTuyenDi.SelectedRows[i].Cells[3].Value.ToString();
                    }
                    
                    DataGridViewRow drow = this.dgvListTuyenDi.SelectedRows[0];
                    DataRowView row = drow.DataBoundItem as DataRowView;
                    this.txtMaTuyen.Text = row[0].ToString();
                    this.txtBenDi.Text = row[1].ToString();
                    this.cbbLoaiXe.Text = row[2].ToString();
                    txtGiaVe.Text = row[3].ToString();
                    this.txtPhuThu.Text = row[4].ToString();
                    //defaul
                    ssThongBao.Text = "Thông báo";
                }
                catch { }
            }
            btCapNhat.Enabled = true;
            btXoa.Enabled = true;
            btThemTuyen.Visible = true;
            btLuu.Visible = false;
        }

        public DataTable LayLoaiXe()
        {
            SqlConnection con = new SqlConnection(@"server=.\SQLEXPRESS; database=QuanLyBanVeXe; integrated security=true");
            DataTable dt = new DataTable();
            string sql = "select LoaiXe from LoaiXe";
            SqlDataAdapter da = new SqlDataAdapter(sql, con);
            da.Fill(dt);
            return dt;
        }

        private void btThemTuyen_Click(object sender, EventArgs e)
        {
            btLuu.Enabled = true;
            btXoa.Enabled = false;
            btCapNhat.Enabled = false;
            txtMaTuyen.Text = "";
            txtGiaVe.Text = "";
            txtPhuThu.Text = "";
            cbbLoaiXe.Text = null;
            dgvListTuyenDi.Enabled = false;
            btThemTuyen.Visible = false;
            btLuu.Visible = true;
            btHuy.Visible = true;
        }

        private void btLuu_Click(object sender, EventArgs e)
        {

            if (this.txtBenDi.Text.Trim().Length == 0)
            {

                this.txtBenDi.Focus();
                MessageBox.Show("Chưa nhập bến đi bến đến", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }

            else
                if (this.cbbLoaiXe.Text.Trim().Length == 0)
                {

                    this.cbbLoaiXe.Focus();
                    MessageBox.Show("Chọn loại xe", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    return;
                }
                else
                    if (this.txtGiaVe.Text.Trim().Length == 0)
                    {
                        this.txtGiaVe.Focus();
                        MessageBox.Show("Nhập giá vé", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        return;
                    }
                    else
                        if (KiemTraTuyen())
                        {
                            txtBenDi.Focus();
                            MessageBox.Show("Tuyến đi đã tồn tại", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                            return;
                        }
                        else
                        {
                            try
                            {
                                this.myConnection.Open();
                                myCommand.CommandText = "insert into TuyenDi values (N'" + txtBenDi.Text + "',N'" + cbbLoaiXe.Text + "','" + Convert.ToDecimal(txtGiaVe.Text)+ "','" + txtPhuThu.Text + "')";
                                myCommand.CommandType = CommandType.Text;
                                myCommand.ExecuteNonQuery();
                                this.myConnection.Close();
                                btThemTuyen.Visible = true;
                                dgvListTuyenDi.Enabled = true;
                                btLuu.Visible = false;
                                ssThongBao.Text = "Đã thêm tuyến đi thành công";
                            }
                            catch
                            {
                                MessageBox.Show("Đăng ký không thành công", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                                ssThongBao.Text = "Thông báo";
                            }

                        }

           LoadList(sender, e);
            
        }
        
        private void txtGiaVe_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!Char.IsDigit(e.KeyChar) && !Char.IsControl(e.KeyChar))
                e.Handled = true;
        }

        private void btCapNhat_Click(object sender, EventArgs e)
        {
            if (this.txtBenDi.Text.Trim().Length == 0)
            {

                this.txtBenDi.Focus();
                MessageBox.Show("Chưa nhập bến đi bến đến", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }

            else
                if (this.cbbLoaiXe.Text.Trim().Length == 0)
                {

                    this.cbbLoaiXe.Focus();
                    MessageBox.Show("Chọn loại xe", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    return;
                }
                else
                    if (this.txtGiaVe.Text.Trim().Length == 0)
                    {
                        this.txtGiaVe.Focus();
                        MessageBox.Show("Nhập giá vé", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        return;
                    }
                    else
                        if (KiemTraTuyenCapNhat())
                        {
                            txtBenDi.Focus();
                            MessageBox.Show("Tuyến đi đã tồn tại", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                            return;
                        }
                        else
                        {
                            try
                            {
                                
                                if (this.myConnection.State == ConnectionState.Closed)
                                    this.myConnection.Open();
                                string ma = dgvListTuyenDi.SelectedRows[0].Cells[1].Value.ToString();
                                //myCommand.CommandText = "Update TuyenDi set BenDiBenDen=N'" + txtBenDi.Text + "',LoaiXe=N'" + cbbLoaiXe.Text + "',GiaVe='" + Convert.ToDecimal(txtGiaVe.Text) + "',PhuThu=N'" + txtPhuThu.Text + "' where MaTuyen = '" + ma + "'";
                                myCommand.CommandText = "if not exists (select * from TuyenDi where BenDiBenDen = N'" + txtBenDi.Text + "' and LoaiXe = N'" + cbbLoaiXe.Text + "') Update TuyenDi set BenDiBenDen=N'" + txtBenDi.Text + "',LoaiXe=N'" + cbbLoaiXe.Text + "',GiaVe='" + Convert.ToDecimal(txtGiaVe.Text) + "',PhuThu=N'" + txtPhuThu.Text + "' where MaTuyen = '" + ma + "' else Update TuyenDi set GiaVe='" + Convert.ToDecimal(txtGiaVe.Text) + "',PhuThu=N'" + txtPhuThu.Text + "' where MaTuyen = '" + ma + "'";
                                myCommand.CommandType = CommandType.Text;
                                myCommand.ExecuteNonQuery();
                                this.myConnection.Close();
                                btThemTuyen.Enabled = true;
                                dgvListTuyenDi.Enabled = true;
                                btLuu.Enabled = false;
                                ssThongBao.Text = "Đã cập nhật tuyến đi thành công";
                            }
                            catch
                            {
                                MessageBox.Show("Cập nhật không thành công", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                                ssThongBao.Text = "Thông báo";
                            }

                        }

            LoadList(sender, e);
        }
        private bool KiemTraTuyen()
        {
            for (int i = 0; i < this.dgvListTuyenDi.Rows.Count - 1; i++)
            {

                if (string.Compare(txtBenDi.Text, dgvListTuyenDi.Rows[i].Cells[2].Value.ToString(), true) == 0
                    && string.Compare(cbbLoaiXe.Text, dgvListTuyenDi.Rows[i].Cells[3].Value.ToString(), true) == 0)
                    return true;
            }
            return false;
        }

        private bool KiemTraTuyenCapNhat()
        {
            for (int i = 0; i < this.dgvListTuyenDi.Rows.Count - 1; i++)
            {
                if (txtBenDi.Text.CompareTo(ben) == 0 && cbbLoaiXe.Text.CompareTo(loaixe) == 0)
                    return false;
                if (string.Compare(txtBenDi.Text, dgvListTuyenDi.Rows[i].Cells[2].Value.ToString(), true) == 0
                    && string.Compare(cbbLoaiXe.Text, dgvListTuyenDi.Rows[i].Cells[3].Value.ToString(), true) == 0)
                    return true;
            }
            return false;
        }
        private void btHuy_Click(object sender, EventArgs e)
        {
            dgvListTuyenDi.Enabled = true;
            btLoad_Click(sender, e);
            btHuy.Visible = false;
            btThemTuyen.Visible = true;
            btLuu.Visible = false;
        }

        private void btXoa_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("Bạn muốn xóa tuyến đi đã chọn?", "Xác nhận", MessageBoxButtons.YesNo, MessageBoxIcon.Warning) == DialogResult.Yes)
            {
                for (int i = dgvListTuyenDi.SelectedRows.Count - 1; i >= 0; i--)
                {
                    try
                    {
                        string ma = dgvListTuyenDi.SelectedRows[i].Cells[1].Value.ToString();
                        if (this.myConnection.State == ConnectionState.Closed)
                            this.myConnection.Open();
                        myCommand.CommandText = "delete from VeXe where MaTuyen ='" + ma + "' delete from TuyenDi where MaTuyen ='" + ma + "'";
                        myCommand.ExecuteNonQuery();
                        myConnection.Close();
                        DataGridViewRow row = this.dgvListTuyenDi.SelectedRows[i];
                        dgvListTuyenDi.Rows.Remove(row);
                    }
                    catch { MessageBox.Show("Xóa không thành công"); }
                }
                ssThongBao.Text = "Đã xóa thành công";
                ssCount.Text = " || Số tuyến: " + (dgvListTuyenDi.RowCount - 1).ToString();
                MacDinh();

            }
        }
        private void MacDinh()
        {
            txtBenDi.Text = "";
            cbbLoaiXe.Text = null;
            txtGiaVe.Text = "";
            txtPhuThu.Text = "";
        }
       
        private void rbonTimKiem_Click(object sender, EventArgs e)
        {
            
            
            
        }

        private void btSearch_Click(object sender, EventArgs e)
        {
            if (txtTimMaTuyen.Text.Trim().Length == 0 && cbbTimLoaiXe.Text.Trim().Length == 0 && txtTimBen.Text.Trim().Length == 0)
            {
                ssThongBao.Text = "Nhập dữ liệu cần tìm";

            }
            else
            {
                
                myCommand.CommandText = "select * FROM TuyenDi where MaTuyen like N'" + "%" + txtTimMaTuyen.Text + "%" + "' and BenDiBenDen like N'" + "%" + txtTimBen.Text + "%" + "' and LoaiXe like N'" + "%" + cbbTimLoaiXe.Text + "%" + "'";
                myCommand.CommandType = CommandType.Text;
                if (this.myConnection.State == ConnectionState.Closed)
                    this.myConnection.Open();
                myDataAdapter = new SqlDataAdapter(myCommand);
                DataSet ds = new DataSet();
                myDataAdapter.Fill(ds, "TuyenDi");
                this.dgvListTuyenDi.DataSource = ds.Tables[0];
                if (dgvListTuyenDi.RowCount == 1)
                {
                    ssThongBao.Text = "Không tìm thấy";
                    this.myConnection.Close();

                }
                else
                    ssThongBao.Text = "Đã tìm thấy " + (dgvListTuyenDi.RowCount - 1).ToString() + " vé";
            }
        }

        private void btClear_Click(object sender, EventArgs e)
        {
            btLoad_Click(sender, e);
            dgvListTuyenDi_Click(sender, e);
            txtTimBen.Text = txtTimMaTuyen.Text = "";
            cbbTimLoaiXe.Text = null;

        }

       
        private void buttonItem7_Click(object sender, EventArgs e)
        {
            FormMain frm = new FormMain();
            frm.Show();
            
        }

        private void btPrint_Click(object sender, EventArgs e)
        {
            ReportTuyenDi frm = new ReportTuyenDi();
            frm.ShowDialog();
            //DataGridViewCheckBoxCell chk = (DataGridViewCheckBoxCell)Row.Cells[1];
            //string print = "True";
            ////if(
            //try
            //{

            //    if (this.myConnection.State == ConnectionState.Closed)
            //        this.myConnection.Open();
            //    string ma = dgvListTuyenDi.SelectedRows[0].Cells[1].Value.ToString();
            //    myCommand.CommandText = "Update TuyenDi set BenDiBenDen=N'" + txtBenDi.Text + "',LoaiXe=N'" + cbbLoaiXe.Text + "',GiaVe='" + Convert.ToDecimal(txtGiaVe.Text) + "',PhuThu=N'" + txtPhuThu.Text + "',Printt= where MaTuyen = '" + ma + "'";
            //    //myCommand.CommandText = "if not exists (select * from TuyenDi where BenDiBenDen = N'" + txtBenDi.Text + "' and LoaiXe = N'" + cbbLoaiXe.Text + "') Update TuyenDi set BenDiBenDen=N'" + txtBenDi.Text + "',LoaiXe=N'" + cbbLoaiXe.Text + "',GiaVe='" + Convert.ToDecimal(txtGiaVe.Text) + "',PhuThu=N'" + txtPhuThu.Text + "' where MaTuyen = '" + ma + "' else Update TuyenDi set GiaVe='" + Convert.ToDecimal(txtGiaVe.Text) + "',PhuThu=N'" + txtPhuThu.Text + "' where MaTuyen = '" + ma + "'";
            //    myCommand.CommandType = CommandType.Text;
            //    myCommand.ExecuteNonQuery();
            //    this.myConnection.Close();
            //    btThemTuyen.Enabled = true;
            //    dgvListTuyenDi.Enabled = true;
            //    btLuu.Enabled = false;
            //    ssThongBao.Text = "Đã cập nhật tuyến đi thành công";
            //}
            //catch
            //{
            //    MessageBox.Show("Cập nhật không thành công", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
            //    ssThongBao.Text = "Thông báo";
            //}
            //LoadList(sender, e);
        }

        private void rbonTuyenDi_Click(object sender, EventArgs e)
        {

        }

        private void dgvListTuyenDi_RowEnter(object sender, DataGridViewCellEventArgs e)
        {
            dgvListTuyenDi_Click(sender, e);
        }

        private void buttonItem6_Click(object sender, EventArgs e)
        {
            ReportTuyenDi frm = new ReportTuyenDi();
            frm.ShowDialog();
        }

       
      
       
    }
}
